package com.universest.swordholder.data.dao

import androidx.room.*
import kotlinx.coroutines.flow.Flow

@Entity(tableName = "volume")
data class Volume(
    @PrimaryKey(autoGenerate = true) val volumeId: Int = 0,
    val name: String,
    val order: Int,
    val bookId: Int,
    val extInfo: String = "",
    val summaryId: Int = -1,
    val summaryUse: Boolean = false
)

@Dao
interface VolumeDao {

    @Insert(onConflict = OnConflictStrategy.REPLACE)
    suspend fun insert(volume: Volume): Long

    @Update
    suspend fun update(volume: Volume)

    @Delete
    suspend fun delete(volume: Volume)

    @Query("SELECT * FROM volume WHERE volumeId = :volumeId")
    fun getVolumeById(volumeId: Int): Flow<Volume?>

    @Query("SELECT * FROM volume ORDER BY [order] ASC")
    fun getAllVolumes(): Flow<List<Volume>>

    @Query("DELETE FROM volume WHERE volumeId = :volumeId")
    suspend fun deleteById(volumeId: Int)

    // 新增方法
    @Query("SELECT * FROM volume WHERE bookId = :bookId ORDER BY [order]")
    fun getVolumeByBookId(bookId: Int): Flow<List<Volume>>

    @Query("DELETE FROM volume WHERE bookId = :bookId")
    suspend fun deleteByBookId(bookId: Int)

}
